const app = getApp()
const ajax = app.myRequest()
import storage from "../../utils/storage.js"
Page({
    data: {
        list: [],
        page: 0,
        total: 0,
        type: 'sanri',
        longitude: null,
        latitude: null,
        user: null
    },

    onLoad: function (options) {
        var that = this

        if (options.type == 'youhui') that.setData({ type: 'youhui' })

        // 获取定位
        wx.getLocation({
            success(res) {
                let latitude = res.latitude
                let longitude = res.longitude
                that.setData({ latitude, longitude })
                that.onReachBottom()
            }
        })
        ajax.getRequest('/User/profile').then((res) => {
            that.setData({ user: res.data.data })
        })
    },

    // 下拉刷新
    onPullDownRefresh: function () {
        this.setData({ list: [], page: 0 })
        this.onLoad({ type: this.data.type })
    },

    // 上拉加载
    onReachBottom: function () {
        var that = this
        let page = that.data.page + 1
        let latitude = that.data.latitude
        let longitude = that.data.longitude
        let type = that.data.type
        that.setData({ latitude, longitude })
        ajax.getRequest('/Gymnasium/getActivityDiscount', { longitude, latitude, type, page }).then((res) => {
            let temp = that.data.list.concat(res.data.data.data)
            that.setData({ page: res.data.data.current_page, list: temp, total: res.data.data.total })
            wx.stopPullDownRefresh()
        })
    },

    // 去场馆主页
    goVenue(e) {
        var index = e.currentTarget.dataset.id
        var id = this.data.list[index].id
        wx.navigateTo({
            url: '/pages/venue/venueHome/index?id=' + id,
        })
    },

    // 出示三日卡二维码
    dianjisanrika: function () {
        let that = this
        if (that.data.shareCode_ == null) {
            var page = 'pages/personal/systemSettings/index'
            var scene = that.data.user.reward.id + '&' + 2
            var width = 300
            ajax.postRequest('/Content/qrcode', { page, scene, width }).then((res) => {
                if (res.data.code == 1) {
                    that.setData({ shareCode_: res.data.data, shareCode: !0 })
                } else {
                    wx.showToast({ title: 'ServerError：' + res.data.msg, icon: 'none' })
                }
            })
        } else {
            that.setData({ shareCode: !0 })
        }
    },

    // 关闭三日卡二维码
    closeShareCode: function () {
        this.setData({ shareCode: !1 })
    }
})